package mango.web;

import java.util.List;

import mango.Mango;
import mango.functions.FuncString;

public class InfoMaker {
    private InfoMaker maker = null;
    private static final InfoMaker INFO_MAKER = new InfoMaker();
    
    private InfoMaker() {
    }
    
    public static InfoMaker getInstance() {
        return INFO_MAKER;
    }

    public String makeInfo(List<Info> infoList) {
        if (infoList != null && infoList.size() != 0) {
            return infoList.get(0).toJSON();
        }
        return "";
    }
    
    public final Info generateInfo(int type, String msg) {
        boolean success = false;
        switch (type) {
        case Info.SUCCESS:
        case Info.CONFIRM:
        case Info.INFO:
            success = true;
            break;
        case Info.TIMEOUT:
        case Info.WARNING:
        case Info.ERROR:
        default:
            success = false;
            break;
        }
        Info info = new Info(type, success, this.getInfo(msg));
        return info;
    }
    
    public String getInfo(String msgId) {
        String infoMaker = Mango.getProperty(Mango.INFO_MAKER);
        if (FuncString.isBlank(infoMaker)) {
            return msgId;
        }
        try {
            if(this.maker == null) {
                Class<?> cls = Class.forName(infoMaker);
                this.maker = (InfoMaker) cls.newInstance();
            }
            return this.maker.getInfo(msgId);
        }
        catch (Exception e) {
            return msgId;
        }
    }
}
